package utils
{
	import mx.utils.StringUtil;

	public class SMStringUtils extends StringUtil
	{
		
		static private var acentos:Array = ['á', 'à', 'ã', 'â', 'ä',
			'é', 'è', 'ê', 'ë', 'í', 'ì', 'î', 'ï', 'ó', 'ò', 'õ', 'ô', 'ö',
			'ú', 'ù', 'û', 'ü', 'ñ', 'ý', 'ç', '~', '^', '´', '`', '¨', '@', 
			'#', '$', '%', '&', '*', '§', '\'', '£', '¢', '¬', '°', '¹', '²', '³'];
		
		static private var semAcentos:Array = ['a', 'a', 'a', 'a',
			'a', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o',
			'o', 'u', 'u', 'u', 'u', 'n', 'y', 'c', ' ', ' ', ' ', ' ', ' ', 
			' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
		
		public function SMStringUtils()
		{
			super();
		}
		
		static public function removeCaracteresEspeciais(...params) : String 
		{
			
			if (SMUtils.isNullOrEmpty(params)) 
			{
				return null;
			}
			
			var retorno:String;
			
			for (var i:int = 0; i < params.length; i++) 
			{
				
				if (params[i] != null && params[i] is String) 
				{
					retorno = params[i].toLowerCase();
					
					for (var j:int = 0; j < acentos.length; j++) 
					{
						retorno = replace(retorno, acentos[j], semAcentos[j]);
					}
					
				}
				
			}
			
			return trim(retorno);
			
		}
		
		static public function replaceAll(str:String, from:String, to:String) : String 
		{
			var idx:int = str.indexOf( from );
			
			while ( idx > -1 ) 
			{
				str = replace(str, from, to ); 
				idx = str.indexOf( from );
			}
			
			return trim(str);
			
		}
		
		static public function replace(str:String, oldSubStr:String, newSubStr:String) : String 
		{
			return str.split(oldSubStr).join(newSubStr);
		}
		
		static public function trim(str:String):String 
		{
			if (str == null) 
			{
				return str;
			}
			
			return StringUtil.trim(str);
		}
		
		static public function lpad(str:String, length:int, ch:String=" "):String
		{
			if (str.length >= length)
			{
				return str;
			}
			
			if (ch.length != 1)
			{
				throw new SyntaxError("ch.length must be 1.");
			}
			
			return repeat(ch, length - str.length) + str;
			
		}
		
		static public function rpad(str:String, length:int, ch:String=" "):String
		{
			if (str.length >= length)
			{
				return str;
			}
			
			if (ch.length != 1)
			{
				throw new SyntaxError("ch.length must be 1.");
			}
			
			return str + repeat(ch, length - str.length);
		}
		
		static public function contains(p_string:String, p_char:String):Boolean
		{
			if (p_string == null)
			{
				return false;
			}
			return p_string.indexOf(p_char) != -1;
		}
		
		static public function countOf(p_string:String, p_char:String, p_caseSensitive:Boolean=true):uint
		{
			if (p_string == null)
			{
				return 0;
			}
			
			var char:String=escapePattern(p_char);
			var flags:String=(!p_caseSensitive) ? 'ig' : 'g';
			
			return p_string.match(new RegExp(char, flags)).length;
			
		}
		
		static public function endsWith(p_string:String, p_end:String):Boolean
		{
			return p_string.indexOf(p_end) == p_string.length - p_end.length;
		}
		
		static public function remove(p_string:String, p_remove:String, p_caseSensitive:Boolean=true):String
		{
			if (p_string == null)
			{
				return '';
			}
			var rem:String=escapePattern(p_remove);
			var flags:String=(!p_caseSensitive) ? 'ig' : 'g';
			return p_string.replace(new RegExp(rem, flags), '');
		}
		
		static public function reverseWords(p_string:String):String
		{
			if (p_string == null)
			{
				return '';
			}
			return p_string.split(/\s+/).reverse().join('');
		}
		
		static public function wordCount(p_string:String):uint
		{
			if (p_string == null)
			{
				return 0;
			}
			return p_string.match(/\b\w+\b/g).length;
		}
		
		static private function escapePattern(p_pattern:String):String
		{
			return p_pattern.replace(/(\]|\[|\{|\}|\(|\)|\*|\+|\?|\.|\\)/g, '\\$1');
		} 
		
	}
}